package io.gitee.chearnee.fw.web.config;


import io.gitee.chearnee.fw.common.component.*;
import io.gitee.chearnee.fw.common.model.IBaseEnum;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * springboot 的Jackson配置 此配置开启了@EnableMvc 可能不生效，需要去实现 WebMvcConfigurer 并在其中配置
 *
 * @see FWConfig
 */
@Configuration
public class JacksonBaseConfig {

    private static void customize(Jackson2ObjectMapperBuilder builder) {
        builder
                .serializerByType(LocalDateTime.class, new CustomLocalDateTimeSerializer())
                .deserializerByType(LocalDateTime.class, new CustomLocalDateTimeDeserializer())
                .serializerByType(LocalDate.class, new CustomLocalDateSerializer())
                .deserializerByType(LocalDate.class, new CustomLocalDateDeserializer())
                .serializerByType(IBaseEnum.class, new BaseEnumSerializer())
                .deserializerByType(IBaseEnum.class, new BaseEnumDeserializer())
        ;
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return JacksonBaseConfig::customize;
    }
}